package com.googlecode.scenemaker3d.view.timeline
{
	import flash.display.Sprite;
	import flash.text.TextField;

	public class Grid extends Sprite
	{
		private var sqr:uint;
		private var gridNumSpacing:uint;
		private var gridNumbers:Sprite = new Sprite();
		
		public function Grid(w:uint, h:uint, sqr:uint, gridNumSpacing:uint)
		{
			super();
			
			this.sqr = sqr,
			this.gridNumSpacing = gridNumSpacing;
			
			this.addChild(gridNumbers);
			
			makeGrid(w, h, sqr, gridNumSpacing);
		}
		public function makeGrid(w:uint, h:uint, sqr:uint, gridNumSpacing:uint):void
		{
			this.graphics.clear();
						
			//Vertical lines
			 for(var i:uint=0; i<w+1; i++)
			{
				if(i%gridNumSpacing == 0 || i%gridNumSpacing == 1)
				{
					this.graphics.moveTo(i*sqr, -3);
					this.graphics.lineStyle(2, 0x7777777);
				}else
				{
					this.graphics.moveTo(i*sqr, 0);
					this.graphics.lineStyle(1, 0x999999);
				}
				
				this.graphics.lineTo(i*sqr, sqr*h);
			} 
			//Horizontal lines
			this.graphics.lineStyle(1, 0x999999);
			
			for(i=0; i<h+1; i++)
			{
				this.graphics.moveTo(0, i*sqr);
				this.graphics.lineTo(w, i*sqr);
			} 
			makeGridNumbers(w);
		}
		private function makeGridNumbers(w:uint):void
		{
			//Start with a clean sheet
			for(var i:uint=0; i<gridNumbers.numChildren; i++)
			{
				if(gridNumbers.getChildAt(i).name == "number")
				{
					gridNumbers.removeChildAt(i);
					i--;
				}
			}
			var no:TextField;
			var intervals:uint = w / gridNumSpacing;

			for(i=0; i<intervals; i++)
			{
				no = new TextField();
				no.name = "number";
				no.text = String(i);
				no.x = i * gridNumSpacing * sqr;
				no.y = no.y - 18;
				
				gridNumbers.addChild(no);
			}			
		}
	}
}